﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dcms.BLL;
using Dcms.Model;
using Dcms.Common;
using Dcms.TempLate;

public partial class vote : System.Web.UI.UserControl
{
    Dcms_CategoryBLL cBll = new Dcms_CategoryBLL();
    Dcms_VoteBLL vBll = new Dcms_VoteBLL();
    public string[] StyleArr;
    //模板数
    public int TempTotalNum;
    //要展示几条
    public int ItemTotalNum;
    //实际展示的数据条数
    public int ActualNum;
    //Dcms Repeater的一些属性
    public string IsPage = "FALSE";
    public string Condition = string.Empty;
    public string Order = "ID";
    public string Sort = "Desc";
    public string TotalNum = "0";
    //数据库查询语句
    public string Sqlstr = "Select * From Dcms_Vote Where Vote_State=1";
    public string SqlNum = "Select Count(Vote_Id) From Dcms_Vote Where Vote_State=1";
    protected void Page_Load(object sender, EventArgs e)
    {
        int Page = 1;
        string KeyWord = Utils.ChkSQL(Request["KeyWord"]);
        int CateId = 0;
        int QueryCateId = DcmsRequest.GetQueryInt("CateId", 0);
        int Id = DcmsRequest.GetQueryInt("Id", 0);

        //处理排序属性
        if ((vote_Repeater.SetOrder.ToUpper() != "ID") && (vote_Repeater.SetOrder.Length > 0))
        {
            Order = vote_Repeater.SetOrder.ToUpper();
        }
        //取排序的字段
        switch (Order)
        {
            case "ID":
                Order = "vote_Id";
                break;
            case "TIME":
                Order = "vote_AddTime";
                break;
            case "ORDER":
                Order = "vote_Order";
                break;
            default:
                Order = "vote_Id";
                break;
        }
        //处理升序或者降序
        if ((vote_Repeater.SetSort.ToUpper() != "DESC") && (vote_Repeater.SetOrder.Length > 0))
        {
            Sort = "Asc";
        }
        //取默认条件
        if (vote_Repeater.SetCondition.Length > 0)
        {
            CateId = Utils.StrToInt(vote_Repeater.SetCondition, 0);
            if (CateId == 0)
            {
                string SplitCondition = vote_Repeater.SetCondition.ToUpper();
                string[] ConditionArr = SplitCondition.Split(',');
                //是否有默认值
                string CateCondition = string.Empty;
                int Cate_i = 1;
                for (int i = 0; i < ConditionArr.Length; i++)
                {
                    if (Utils.StrToInt(ConditionArr[i], 0) > 0)
                    {
                        if (Cate_i == 1)
                        {
                            CateCondition = Utils.StrToInt(ConditionArr[i], 0).ToString();
                            Cate_i = Cate_i + 1;
                        }
                        else
                        {
                            CateCondition = CateCondition + "," + Utils.StrToInt(ConditionArr[i], 0);
                        }
                    }
                }
                //是否有默认值结束
                //由Query取值
                if (SplitCondition.IndexOf("QUERY") >= 0)
                {
                    if (QueryCateId > 0)
                    {
                        if (cBll.GetCateIsChild(QueryCateId) == 0)
                        {
                            string cateString = cBll.GetChildCateId(QueryCateId);
                            if (cateString.Length > 0)
                            {
                                Condition = Condition + " And Vote_CateId In (" + cateString + ")";
                            }
                        }
                        else
                        {
                            Condition = Condition + " And Vote_CateId=" + QueryCateId;
                        }
                    }
                    //处理是否有查询
                    if (KeyWord.Length > 0)
                    {
                        KeyWord = String.Format(" Vote_Title Like '%{0}%'", KeyWord);
                        Sqlstr = Sqlstr + " And " + KeyWord;
                        SqlNum = SqlNum + " And " + KeyWord;
                    }
                }
                if (Condition.IndexOf("Vote_CateId") >= 0)
                {
                    Condition = Condition;
                }
                else
                {
                    if (CateCondition.Length > 0)
                    {
                        Condition = Condition + " And Vote_CateId In (" + CateCondition + ")";
                    }
                    else
                    {
                        Condition = Condition;
                    }
                }
            }
            else
            {
                //Condition = Condition + " And Vote_CateId=" + CateId;
                if (cBll.GetCateIsChild(Utils.StrToInt(CateId, 0)) == 0)
                {
                    Condition = Condition + " And Vote_CateId In(" + cBll.GetChildCateId(Utils.StrToInt(CateId, 0)) + ")";
                }
                else
                {
                    Condition = Condition + " And Vote_CateId=" + CateId;
                }
            }
        }  
        
        //处理分页
        if (vote_Repeater.SetIsPage.ToUpper() != "FALSE")
        {
            IsPage = "True";
        }
        //处理风格字符串
        StyleArr = this.vote_Repeater.SetStyleStr.Split('|');
        //风格种类
        TempTotalNum = StyleArr.Length;
        //默认取多少条
        ItemTotalNum = Utils.StrToInt(vote_Repeater.SetNum, 0);
        //实际取多少条
        ActualNum = (TempTotalNum > ItemTotalNum) ? TempTotalNum : ItemTotalNum;
        //是否是按Id查询
        if (Id > 0 && ActualNum == 1)
        {
            Sqlstr = Sqlstr + " And Vote_Id=" + Id;
            SqlNum = SqlNum + " And Vote_Id=" + Id;
        }

        if (Condition.Length > 0)
        {
            Sqlstr = Sqlstr + Condition;
            SqlNum = SqlNum + Condition;
        }
        //Sql语句合并
        Sqlstr = Sqlstr + " Order By " + Order + " " + Sort;

        //是否要分页并作处理
        if (IsPage.ToUpper() == "TRUE")
        {
            Page = DcmsRequest.GetQueryInt("Page", 1);
            TotalNum = vBll.GetDataCount(SqlNum);

            //把总数写入到Cookie
            Utils.WriteCookie("TotalRecord", TotalNum);
            Utils.WriteCookie("PageSize", ActualNum.ToString());
        }
        //取数据并绑定
        vote_Repeater.DataSource = vBll.GetDataListByPage(Sqlstr, Page, ActualNum);
        vote_Repeater.DataBind();
    }
    //数据渲染
    protected void vote_Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Literal voteItemTemplate = (Literal)e.Item.FindControl("voteDisplay");
        Dcms_VoteModel vModel = (Dcms_VoteModel)e.Item.DataItem;
        if (TempTotalNum > 1)
        {//当模板风格大于1时的渲染方式
            if (e.Item.ItemIndex < TempTotalNum)
            {
                voteItemTemplate.Text = TempRender.TempRenderVote(vModel, StyleArr[e.Item.ItemIndex], (e.Item.ItemIndex - 1));
            }
            else
            {
                int TempIndex = e.Item.ItemIndex % TempTotalNum;
                voteItemTemplate.Text = TempRender.TempRenderVote(vModel, StyleArr[TempIndex], (e.Item.ItemIndex - 1));
            }
        }
        else
        {//当模板风格只有1种时的渲染方式
            voteItemTemplate.Text = TempRender.TempRenderVote(vModel, StyleArr[0], (e.Item.ItemIndex - 1));
        }
    }
}
